/**************************************************************************
 *		   Copyright(c)	2006 by	Sunplus	Technology Co.,	Ltd.			  *
 *																		  *
 *	This software is copyrighted by	and	is the property	of Sunplus		  *
 *	Technology Co.,	Ltd. All rights	are	reserved by	Sunplus	Technology	  *
 *	Co., Ltd. This software	may	only be	used in	accordance with	the		  *
 *	corresponding license agreement. Any unauthorized use, duplication,	  *
 *	distribution, or disclosure	of this	software is	expressly forbidden.  *
 *																		  *
 *	This Copyright notice "M U S T"	not	be removed or modified without	  *
 *	prior written consent of Sunplus Technology	Co., Ltd.				  *
 *																		  *
 *	Sunplus	Technology Co.,	Ltd. reserves the right	to modify this		  *
 *	software without notice.											  *
 *																		  *
 *	Sunplus	Technology Co.,	Ltd.										  *
 *	19,	Innovation First Road, Science-Based Industrial	Park,			  *
 *	Hsin-Chu, Taiwan, R.O.C.											  *
 *																		  *
 *	Author:	Ted	Chiu													  *
 **************************************************************************/
#include	"general.h"
#include	"stdio.h"
#include	"disp_common.h"
#include	"disp_api.h"
#include	"disp_icb.h"
#include	"panel.h"

#define	R3_OF_R61200				0x1900
#define	FORCE_CACHE_SPI_CTRL_CODE	0		//FOR SAMSUNG DEBUG

extern	pSpiFun	dispSpiCtrl;

void	dispLTV160Q1Flip(UINT32 mode);


__inline__ static void	dispSamsungSpiWriteExt(idx, value)
{
	dispSpiCtrl(0x740000+idx,  24,0);
	dispSpiCtrl(0x760000+value,24,0);
}

/*
LTV160Q1:
490*240, 96/9 MHz, R61200
*/
UINT32	dispLTV160Q1Set1(UINT32 panel)
{
	if(panel != LTV160Q1)
		return(-1);
	dispDeviceNameSet("LTV160Q1");
	PanelDimW   = 490;
	PanelDimH   = 240;
	PanelTotalX = 99+492+33;
	PanelTotalY = 22+240+4;
	PanelStartX = 35-1;
	PanelStartY = 22;
	return(panel);
}


UINT32	dispLTV160Q1Set2(UINT32 panel)
{
	if(panel != LTV160Q1)
		return(-1);
	UINT32 i;
	dispReg_t *pdispReg = (dispReg_t *)(0xb000a000);
	dispPnlVhSet(PanelTotalX, PanelTotalY, PanelStartX, PanelStartY,
		PanelStartX+PanelDimW-1, PanelStartY+PanelDimH-1);
	pdispReg->vsyncWidth = 3;	/* 1~3 H */
	pdispReg->hsyncWidth = 15;	/* 9~15 CLK */
	dispPnlTypeSet(panel);
	DISP_CLK_DIV_96_192(9);		/* 96 / 9 = 10.67 MHz */
	LAYOUT_MODE(0);
	dispColorSet(0x0D);
	dispEn(0x01);
	dispRegUpd(0x01);
	dispSpiClkStart(1);

/* init function */
	#if	FORCE_CACHE_SPI_CTRL_CODE
	dispSpiCtrl(0x740000+0, 24,0);
	#endif
	dispSamsungSpiWriteExt(0, 0x0000);
	dispSamsungSpiWriteExt(1, 0x0000);
	dispSamsungSpiWriteExt(2, 0x0000);
	dispSamsungSpiWriteExt(6, 0x00A3);
	dispSamsungSpiWriteExt(5, 0x0000);

/* wait 1 frame or more */
	dispVsyncWait(DISP_SIGNAL_RISING);
	dispVsyncWait(DISP_SIGNAL_RISING);

/* power setting fun 1 */
	dispSamsungSpiWriteExt(2, 0x0910);
	dispSamsungSpiWriteExt(0, 0x0006);

/* wait 3 frame or 40ms */
	for(i=0; i<3; i++)	/* 17*2=34ms */
		dispVsyncWait(DISP_SIGNAL_RISING);

/* power setting fun 2 */
	dispSamsungSpiWriteExt(2, 0x2910);

/* disp setting fun */
	dispSamsungSpiWriteExt(0x00, 0x0016);
	dispSamsungSpiWriteExt(0x03, R3_OF_R61200);
	dispSamsungSpiWriteExt(0x04, 0x7D16);	/* note 1, HBP=99, VBP=22 */
	dispSamsungSpiWriteExt(0x05, 0x3000);
	dispSamsungSpiWriteExt(0x06, 0x0083);
	dispSamsungSpiWriteExt(0x07, 0x0300);
	dispSamsungSpiWriteExt(0x08, 0x0003);
	dispSamsungSpiWriteExt(0x09, 0x0908);
	dispSamsungSpiWriteExt(0x0A, 0x0809);
	for(i=0x0B; i<=0x10; i++)
		dispSamsungSpiWriteExt(i, 0x0303);

/* wait 4 frame or 60ms */
	for(i=0; i<=4; i++)
		dispVsyncWait(DISP_SIGNAL_RISING);

/* display on function */
	dispSamsungSpiWriteExt(0x00, 0x0816);	/* R0, select combination of correspondence */
	dispSamsungSpiWriteExt(0x05, 0x3010);	/*     between RGB dots and source output */
	dispSamsungSpiWriteExt(0x06, 0x1083);
	dispSamsungSpiWriteExt(0x01, 0x0200);
	panelFlipFunc = dispLTV160Q1Flip;
	return(panel);
}

#if	SPCA==5000

UINT32   disp5kLTV160Q1Set(UINT32 panel)
{
	UINT32 ret;
	ret = dispLTV160Q1Set1(panel);
	if(ret != -1)
	{
		disp5kCapabSet(PanelDimW,PanelDimH,PanelStartX,PanelStartY,\
			(panel & 0x0F)<=1);
		disp5kPanelInitFunSet(dispLTV160Q1Set2, panel);
	}
	else
		disp5kPanelInitFunSet(NULL, panel);
	return(ret);
}
#else

UINT32	dispLTV160Q1Set(UINT32 panel)
{
	UINT32 ret;
	ret = dispLTV160Q1Set1(panel);
	ret = dispLTV160Q1Set2(panel);
	return(ret);
}
#endif



void	dispLTV160Q1Flip(UINT32 mode)
{
#if	SPCA53X
	UINT32	regvalue = R3_OF_R61200;	/* only support R/L flipped mode */
	if(mode > 3)
		mode = 0;
	if(mode & DISP_FLIP_H)	{	/* Left/Right Flipped */
		regvalue &= ~0x0100;
	}
	if(mode & DISP_FLIP_V)	{	/* Up/Down Flipped */
		/* was not supported by this panel */
	}
	dispSamsungSpiWriteExt(0x03, regvalue);
#endif
}

